package read_input;

import java.util.ArrayList;

import tree.Tree;
import model.Data;
import model.Stock;
import GP.GPA;
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length < 5){
			System.out.println("wrong args");
			 
		}else{
			new Input(args);
			if (Data.get().getPhase() == Data.Phase.Train){
				GPA gp = new GPA();
				Tree theBest = null;
				//for (int i=0;i<1;i++){
					gp.initPopulation();
					theBest = gp.runGPAlgorithm();
					//System.out.println(theBest);
					//if (i==0){
					theBest.saveTreeToFile();
//					}else{
//						Tree savedTree=Data.get().getSavedTree();
//						if (savedTree.getFitness()<theBest.getFitness()){
//							theBest.saveTreeToFile();
//						}else{
//							theBest=savedTree;
//						}
//					}
					//System.out.println("the fitness of the best:"+ theBest.getFitness()) ;
					//System.out.println( theBest );	
//					System.out.println("real profit: " + calcRealProfit(theBest));
				//}	
			}else{
				Result result = new Result();
				setStocksToBuyFromTree(result);
				result.closeFile();
			}
		}
	}

	/* 
	 * get the best tree from modelFile
	 * check each stock if buy or not to buy. 
	 */
	private static void setStocksToBuyFromTree(Result result) {
		Tree theBest = Data.get().getSavedTree();
		if (theBest.getHead()==null){
			return;
		}
		for ( Stock s: Data.get().getAllStocks() ){
			Data.get().setCurrentStock(s);
			boolean ans = theBest.getAdvice(); //for each stock activate our Best Tree
			if (ans==true){					   // if it say to buy --> buy the stock. 
				result.writeTofile(s.getStockName());
			}
		}
		
//		ArrayList<Stock> buyStocks = new ArrayList<Stock>();
//		Tree theBest = Data.get().getSavedTree();
//		if (theBest.getHead()==null){
//			return buyStocks;
//		}
	}

	private static double calcRealProfit(Tree tree){		
		double realProfit = 0;
		double prophet=0;
		ArrayList<Stock> stocks = new ArrayList<Stock>(); 
		for ( Stock s: Data.get().getAllStocks() ){
			Data.get().setCurrentStock(s);
			boolean ans =  tree.getAdvice();
			double priceStartDay = s.getClosePrice().get(s.getClosePrice().size()-1 - Data.get().getDays());
			double endDay=s.getClosePrice().get(s.getClosePrice().size()-1);
			
			if (endDay-priceStartDay>0){
				prophet+=endDay-priceStartDay;
			}
			if (ans){
				stocks.add(s);
				realProfit += s.getClosePrice().get(s.getClosePrice().size()-1) - 
							  s.getClosePrice().get(s.getClosePrice().size()-1 - Data.get().getDays());
			}
			System.out.println(s.getStockName());
			System.out.println("price at starting day : " + priceStartDay);
			System.out.println("price at ending day : " + s.getClosePrice().get(s.getClosePrice().size()-1));
			System.out.println("the tree said price at ending day : " + ans);

		}	
		System.out.println("could have made: "+prophet);
		System.out.println("all Stocks to buy:");
		System.out.println(stocks);
		return realProfit;
	}
	

	
	private static void indexCheck(ArrayList<Stock> buyStocks) {
		int allprofit = 0;
		for ( Stock s: Data.get().getAllStocks() ){
			allprofit += s.getProfit(); 
		}
		int myprofit = 0;
		for ( Stock s: buyStocks ){
			myprofit += s.getProfit(); 
		}
		System.out.println("my profit is: " + myprofit);
		System.out.println("all profit is: " + allprofit);
		System.out.println("the tree is: ");
		Tree theBest = Data.get().getSavedTree();
		System.out.println(theBest);
		System.out.println("the fitness is:" + theBest.getFitness());
		
	}
}
